package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.CommentMapper;
import com.farmpost.server.mapper.ReplyCommentMapper;
import com.farmpost.server.mapper.UserInfoMapper;
import com.farmpost.server.pojo.Comment;
import com.farmpost.server.pojo.ReplyComment;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.pojo.UserInfo;
import com.farmpost.server.service.ICommentService;
import com.farmpost.server.service.IReplyCommentService;
import com.farmpost.server.service.IUserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.lang.model.element.VariableElement;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author king
 * @since 2022-04-08
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private ReplyCommentMapper replyCommentMapper;
    @Autowired
    private IReplyCommentService iReplyCommentService;
    @Autowired
    private IUserInfoService iUserInfoService;

    /**
     * 用户发表评论
     *
     * @param content
     * @return
     */
    @Override
    public RespBean postComment(String content) {
        //评论审核未做
        UserInfo currentUser = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Comment comment = new Comment();
        comment.setTopicId((int) (Math.random() * 100000));//5位
        comment.setTopicType("comment");
        comment.setContent(content);
        comment.setFromUid(currentUser.getUserId());
        comment.setCommentStatus(true);
        comment.setDateTime(LocalDateTime.now());
        int insert = commentMapper.insert(comment);
        if (insert == 1) {
            return RespBean.success("发表评论成功");
        }
        return RespBean.error("发表评论失败");
    }

    /**
     * 按时间查询最新评论
     *
     * @return
     */
    @Override
    public RespBean getCommentByTime(Page<Comment> page) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_status", 1);
        queryWrapper.orderByDesc("date_time");
        Page<Comment> mapIPage = commentMapper.selectPage(page, queryWrapper);
        List<Comment> list = mapIPage.getRecords();
        for (Comment comment : list) {
            Integer fromUid = comment.getFromUid();
            Integer topicId = comment.getTopicId();
            UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("user_id", fromUid));
            userInfo.setUserPassword(null);
            comment.setUserInfo(userInfo);
            Integer count = replyCommentMapper.selectCount(new QueryWrapper<ReplyComment>().eq("comment_id", topicId));
            comment.setReplyCount(count);
            Page<ReplyComment> replyCommentPage = new Page<>(1, 20, false);
            List<ReplyComment> reply = iReplyCommentService.getReplyById(topicId, comment.getTopicType(), replyCommentPage);
            comment.setReply(reply);
        }
        return RespBean.success("成功", list);
    }

    /**
     * 根据userId查询评论
     *
     * @param userId
     * @return
     */
    @Override
    public List<Comment> getCommentById(Integer userId) {
        List<Comment> comments = commentMapper.selectList(new QueryWrapper<Comment>().eq("from_uid", userId).eq("comment_status", 1).orderByDesc("date_time"));
        for (Comment comment : comments) {
            Integer topicId = comment.getTopicId();
            List<ReplyComment> replyComments = replyCommentMapper.selectList(new QueryWrapper<ReplyComment>().eq("comment_id", topicId));
            comment.setReplyCount(replyComments.size());
            comment.setReply(replyComments);
        }
        return comments;
    }

    /**
     * 分页查询
     *
     * @param pageIndex 当前页
     * @return
     */
    @Override
    public RespBean queryComment(Integer pageIndex) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>().eq("comment_status", 1).orderByDesc("date_time");
        PageHelper.startPage(pageIndex, 10);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        for (Comment comment : comments) {
            //获取当前评论下的所有回复
            Integer topicId = comment.getTopicId();
            List<ReplyComment> replyComments = iReplyCommentService.queryReplyByTopicId(topicId);
            comment.setReply(replyComments);
            //获取当前评论的用户信息
            UserInfo userInfo = iUserInfoService.getUserInfoByUserId(comment.getFromUid());
            userInfo.setUserPassword(null);
            comment.setUserInfo(userInfo);
            //设置当前评论下的回复数量
            comment.setReplyCount(replyComments.size());
        }
        return RespBean.success("成功",new PageInfo<>(comments));
    }

    /**
     * 点赞评论
     * @param topicId
     * @return
     */
    @Override
    public RespBean updateGreatCount(Integer topicId) {
        Comment comment = commentMapper.selectOne(new QueryWrapper<Comment>().eq("topic_id", topicId));
        Integer greatCount = comment.getGreatCount();
        Comment insertComment = new Comment();
        insertComment.setGreatCount(++greatCount);
        int update = commentMapper.update(insertComment, new QueryWrapper<Comment>().eq("topic_id", topicId));
        if (update >= 1)
            return RespBean.success("success");
        return RespBean.error("fail");
    }
}
